import sys


def process(n, ann, qnum):
    for i in range(n):
        if qnum in ann[i]:
            all_n = []
            x = ann[i].index(qnum)
            all_n.append(x)
            sn = binarySearch(ann[i], x, len(ann[i])-1, qnum)
            while  sn != -1:
                all_n.append(sn)
                sn = binarySearch(ann[i], sn, len(ann[i])-1, qnum)

            return (i + 1), (all_n[-1] + 1)
    return -1, 0


def binarySearch(arr, l, r, x):
    if r >= l:
        mid = int(l + (r - l) / 2)
        if arr[mid] == x:
            return mid
        elif arr[mid] > x:
            return binarySearch(arr, l, mid - 1, x)
        else:
            return binarySearch(arr, mid + 1, r, x)

    else:
        return -1

try:
    t = int(input())
    if t < 1 or t > 10:
        sys.exit(0)
    for i in range(t):
        n = int(input())
        ann = [[] for i in range(n)]
        for i in range(n):
            temp = list(input().split())
            ann[i] = temp
        q = int(input())
        for i in range(q):
            temp2 = input()
            x, y = process(n, ann, temp2)
            if x == -1:
                print(-1)
            else:
                print(x, y)

except ValueError:
    pass
except EOFError:
    pass
except MemoryError:
    pass
